package com.admin.task;

import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.admin.es.entity.EsSchoolActivityEntity;
import com.admin.es.mapper.EsSchoolActivityMapper;
import com.admin.user.entity.SchoolActivity;
import com.admin.user.mapper.SchoolActivityMapper;
import com.admin.utils.DataTransitionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * 定时任务管理
 * @author 武佳敏
 * @date 2023-02-01
 */
@EnableScheduling
@Component
@Slf4j
public class SynDataMysql2ESTask {

    @Autowired
    SchoolActivityMapper userInfoMapper;

    @Autowired
    EsSchoolActivityMapper esUserInfoMapper;

    /**
     * 数据库同步数据到es定时任务
     * @author 武佳敏
     * @date 2023-02-01
     */
    @Scheduled(fixedRate = 24 * 60 * 60)
    public void syn4User() {
        //查询出数据库中的全部数据，可以根据需求来查询需要的数据进行同步
        List<SchoolActivity> schoolActivities = userInfoMapper.selectList(null);
        log.info("数据查询到的数据:{}",schoolActivities);
        List<EsSchoolActivityEntity> esUserEntities=new ArrayList<>();
        for (SchoolActivity schoolActivity : schoolActivities) {
            //数据库实体转换es实体
            EsSchoolActivityEntity entity=DataTransitionUtils.EntityToEsEntity(schoolActivity);
            esUserEntities.add(entity);
        }
        //开始存入ES
        //mysql数据转为es数据
        Integer insert = esUserInfoMapper.insertBatch(esUserEntities,"school_activity");
        log.info("同步结果:{}",insert);
        List<EsSchoolActivityEntity> esUserEntities1 = esUserInfoMapper.selectList(new LambdaEsQueryWrapper<>());
        log.info("es数据查询到的数据:{}",esUserEntities1);
    }
}
